package code.untils.caurina.transitions
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	/**
	 * 弹跳组件
	 * @author chenyonghua
	 * 
	 */	
	public class Elastic
	{
		private static var INSTANCE:Elastic = null;
		private var queues:Array;
		private var sp:Sprite = new Sprite();
		public function Elastic()
		{			
			queues = [];
		}
		
		public static function getInstance():Elastic
		{
			return INSTANCE ||= new Elastic();
		}
		
		/**
		 * 弹跳队列
		 * @param target      	对象
		 * @param vx			x轴速度
		 * @param vy			y轴速度
		 * @param rect			范围
		 * @param times			弹跳的次数
		 * @param bounce		反弹系数
		 * @param gravity		空气摩擦力
		 * 
		 */				
		public function add(target:DisplayObject,vx:Number,vy:Number,rect:Rectangle,times:int = 3,bounce:Number = -0.7,gravity:Number = 9):void
		{
			var o:ElasticObject = new ElasticObject(target,bounce,gravity,vx,vy,rect,times);
			queues.push(o);
			if(queues.length)
			{
				sp.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			}			
		}
		
		/**
		 *移除 
		 * @param target 对象
		 * 
		 */
		public function remove(target:DisplayObject):void{
			if(target){
				var len:int = queues.length;
				for (var i:int = len-1; i>=0; i--)
				{
					var o:ElasticObject = queues[i];
					if(o.target == target)queues.splice(i,1);
				}
			}
		}
		
		
		private function moveTarget(o:ElasticObject):void{
			
		}
		/**
		 * EnterFrame
		 * @param event
		 * 
		 */		
		protected function onEnterFrame(event:Event):void
		{
			var len:int = queues.length;
			for (var i:int = len-1; i>=0; i--)
			{
				var o:ElasticObject = queues[i];
				if(o.times <= 0)
				{
					queues.splice(i,1);
					return;
				}
				o.vy += o.gravity;
				o.target.x += o.vx;
				o.target.y += o.vy;
				
				var left:Number = o.rect.x;
				var right:Number = o.rect.width;
				var top:Number = o.rect.y;
				var bottom:Number = o.rect.height;
				
				if(o.target.x + o.target.height > right)
				{
					o.target.x = right;
					o.vx *= o.bounce;
				}
				else if(o.target.x < left)
				{
					o.target.x = left;
					o.vx *= o.bounce;
				}
				if(o.target.y > bottom)
				{
					o.target.y = bottom;
					o.vy *= o.bounce;
					o.times--;
				}
				else if(o.target.y < top)
				{
					o.target.y = top;
					o.vy *= o.bounce;
				}
			}
			
			if( queues.length <=0 )
			{
				sp.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
		}
		
	}
}
import flash.display.DisplayObject;
import flash.geom.Rectangle;

class ElasticObject
{
	public var target:DisplayObject;
	public var bounce:Number;
	public var gravity:Number;
	public var vx:Number;
	public var vy:Number;
	public var times:int;
	public var rect:Rectangle;
	public function ElasticObject(
		target:DisplayObject,
		bounce:Number,
		gravity:Number,
		vx:Number,
		vy:Number,
		rect:Rectangle,
		times:int
	)
	{
		this.target 			= target;
		this.rect 				= rect;
		this.bounce 			= bounce;
		this.gravity 			= gravity;
		this.vx 				= vx;
		this.vy 				= vy;
		this.times 				= times;
	}
}